<?php
declare(strict_types=1);

namespace app\model;

use think\Model;

class Area extends Model
{
    protected $autoWriteTimestamp = true;

    // 关联父级地区
    public function parent()
    {
        return $this->belongsTo(Area::class, 'parent_id');
    }

    // 关联子级地区
    public function children()
    {
        return $this->hasMany(Area::class, 'parent_id');
    }

    // 获取省份列表
    public static function getProvinces()
    {
        return self::where('parent_id', 0)->select();
    }

    // 获取城市列表
    public static function getCities(int $provinceId)
    {
        return self::where('parent_id', $provinceId)->select();
    }

    // 获取区县列表
    public static function getCounties(int $cityId)
    {
        return self::where('parent_id', $cityId)->select();
    }

    // 获取完整地区名称
    public function getFullName(): string
    {
        $names = [];
        $area = $this;
        
        while ($area) {
            array_unshift($names, $area->area_name);
            $area = $area->parent;
        }

        return implode(' ', $names);
    }
} 